package view;

import connectivity.QueryManager;
import java.awt.BorderLayout;
import main.WinkelApplication;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import model.Category;

public class CategoryList extends JPanel implements ActionListener, MouseListener {
    private final int verticalPosition = 10;
    private final int offset = 40;
    
    public JPanel viewPanel;
    public JScrollPane sp;

    public CategoryList() {
        super();
        setLayout(null);
        initComponents();
    }

    /** create the gui for this page */
    private void initComponents() {
        addTitle();
        WinkelApplication.mainWindow.setTitle("Catalogus");
        addBasket();
        addBackButton();
        addScrollPane();
        addcategoryItems();
    }
    
    public void addScrollPane() {
        viewPanel = new JPanel(new BorderLayout());
        sp = new JScrollPane(viewPanel);
        sp.setLocation(20, 45);
        sp.setSize(521, 370);
        sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        viewPanel.setSize(521, 370);
        sp.setBorder(null);
        add(sp);
    }
    
    /** add the page's title */
    private void addTitle() {
        JLabel lblTitle = new JLabel();
        lblTitle.setText("Winkelapplicatie");
        lblTitle.setBounds(20, 20, 150, 20);
        lblTitle.setFont(WinkelApplication.FONT_16_BOLD);
        add(lblTitle);
        int btnOffset = getHeight() - 25;
    }
    
    private void addBackButton(){
        JButton btnBack = new JButton("Terug");
        btnBack.setBounds(20,420,150,20);
        btnBack.setFont(WinkelApplication.FONT_12_BOLD); 
        btnBack.addActionListener(this);
        btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgBack.png")));
        add(btnBack);
    }

    /** add the different catergories to the page */
    private void addcategoryItems() {
        QueryManager queryManager = WinkelApplication.getQueryManager();
        List<Category> categories = queryManager.getCategories();
        
        for (int i = 0; i < categories.size(); i++) {
            Category category = categories.get(i);
            JLabel lblDot = new JLabel("\u2022");
            lblDot.setFont(WinkelApplication.FONT_12_BOLD);
            lblDot.setBounds(5, verticalPosition + i * offset, 10, 20);
            viewPanel.add(lblDot);

            JLabel lblCategorie = new JLabel();
            lblCategorie.setName(String.valueOf(category.getCategoryId()));
            lblCategorie.setText(category.getName());
            lblCategorie.setBounds(20, verticalPosition + i * offset, 200, 20);
            lblCategorie.setFont(WinkelApplication.FONT_12_BOLD);
            lblCategorie.addMouseListener(this);
            lblCategorie.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            viewPanel.add(lblCategorie);

            JLabel lblDescription = new JLabel();
            lblDescription.setText(category.getDescription());
            lblDescription.setBounds(20, verticalPosition  + i * offset + 15, 200, 20);
            lblDescription.setFont(WinkelApplication.FONT_10_PLAIN);
            viewPanel.add(lblDescription);
        }
        
        JLabel lblEmpty = new JLabel("");
        viewPanel.add(lblEmpty);
    }

    /** add the basket view into this page */
    
    @Override
    public void actionPerformed(ActionEvent event) {
        WinkelApplication.getInstance().showPanel(new MainMenu());
    }
    
    private void addBasket() {
        BasketDisplay basket = new BasketDisplay();
        this.add(basket);
    }

    @Override
    public void mouseClicked(MouseEvent event) {
        JLabel label = (JLabel) event.getSource();
        int categoryId = Integer.parseInt(label.getName());
        ProductList productList = new ProductList(categoryId);
        WinkelApplication.getInstance().showPanel(productList,795,480);
    }

    @Override
    public void mouseEntered(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void mousePressed(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void mouseReleased(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void mouseExited(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
        graphics.drawLine(20, 45, 540, 45);
    }
}
